<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "order".
 *
 * @property int $order_id 主键id
 * @property int $user_id 买家id
 * @property int $sail_id 卖家id
 * @property int $pid 宠物id
 * @property int $prop_id 道具id
 * @property int $per_price 单价
 * @property int $service_charge 手续费
 * @property int $sail_num 数量
 * @property int $total_price 总共数量
 * @property int $created_at 下单时间
 * @property int $updated_at 更新订单时间
 */
class Order extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'order';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_id', 'sail_id', 'pid', 'prop_id', 'per_price', 'service_charge', 'sail_num', 'total_price', 'created_at', 'updated_at'], 'integer'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'order_id' => '主键id',
            'user_id' => '买家id',
            'sail_id' => '卖家id',
            'pid' => '宠物id',
            'prop_id' => '道具id',
            'per_price' => '单价',
            'service_charge' => '手续费',
            'sail_num' => '数量',
            'total_price' => '总共数量',
            'created_at' => '下单时间',
            'updated_at' => '更新订单时间',
        ];
    }

    /**
     * 宠物订单增加
     * @param $userPetRow
     * @return bool
     */
    public function addPetUserOrder($userPetRow)
    {
        if (empty($userPetRow)) {
            return false;
        }
        $serviceFee = round($userPetRow->price * 0.05);//手续费金额
        $incomme = $userPetRow->price - $serviceFee;//卖家收入
        $userId = Yii::$app->user->getId();
        $order = new Order();
        $order->user_id = $userId;
        $order->sail_id = $userPetRow->user_id;
        $order->pid = $userPetRow->pid;
        $order->prop_id = 0;
        $order->per_price = $incomme;
        $order->service_charge = $serviceFee;
        $order->sail_num = 1;
        $order->total_price = $incomme;
        $order->created_at = $order->updated_at = time();
        return $order->save();//生成订单
    }
}
